package tree_util;

import com.common.FishEPTool;
import com.common.WaterQualityDetectionTool;
import com.opensymphony.xwork2.ActionSupport;
import db_tool.LogTool;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by 10742 on 2017/7/8.
 */
public class Op_FishEP extends ActionSupport {

    int id;
    int indexID;
    String indexName;
    String indexValue;
    String tissue;
    String sampleID;
    String flag;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getIndexID() {
        return indexID;
    }

    public void setIndexID(int indexID) {
        this.indexID = indexID;
    }

    public String getIndexName() {
        return indexName;
    }

    public void setIndexName(String indexName) {
        this.indexName = indexName;
    }

    public String getIndexValue() {
        return indexValue;
    }

    public void setIndexValue(String indexValue) {
        this.indexValue = indexValue;
    }

    public String getTissue() {
        return tissue;
    }

    public void setTissue(String tissue) {
        this.tissue = tissue;
    }

    public String getSampleID() {
        return sampleID;
    }

    public void setSampleID(String sampleID) {
        this.sampleID = sampleID;
    }

    public String getFlag() {
        return flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }

    @Override
    public String execute() throws IOException {
        ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");
        FishEPTool tool = (FishEPTool) context.getBean("fishEPTool");
        ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
        PrintWriter writer = ServletActionContext.getResponse().getWriter();

        System.out.println(indexID + " " + indexName + " " + indexValue + " " + tissue + " " + sampleID);
        if (flag.equals("insert")) {
            id = tool.insert(indexID, indexName, indexValue, tissue, sampleID);
            writer.write(String.valueOf(id));
            LogTool.insertIntoDataLog(LogTool.INSERT_CODE, "FishResidEnvirPollu_Analysis", 1);
            writer.close();
        } else if (flag.equals("update")) {
            int column = tool.update(id, indexID, indexName, indexValue, tissue);
            if (column == 1){
                writer.write("true");
                LogTool.insertIntoDataLog(LogTool.UPDATE_CODE, "FishResidEnvirPollu_Analysis", 1);
            }
            writer.close();
        }
        return null;
    }
}
